package com.example.xinlijiankangsys.util;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.TypeReference;
import com.example.xinlijiankangsys.pojo.WebSocketRequestData;
import com.example.xinlijiankangsys.pojo.WebSocketRequestDataMateResult;
import com.example.xinlijiankangsys.service.WebSocketService;

import java.util.HashMap;
import java.util.Map;
public class DataProcessingUtil {
    private static final org.slf4j.Logger log
            = org.slf4j.LoggerFactory.getLogger(WebSocketService.class);


    public static final Map<String, String[]> mate = new HashMap<>() {{
        put("SEND_MSG",new String[]{ "sender", "receiver", "timestamp", "content","tempId"});
        put("ANONYMOUS_MSG",new String[]{ "sender", "receiver", "timestamp", "content","tempId"});
        //anonymous
    }};
    public static WebSocketRequestData processingRequestData(String json){
        WebSocketRequestData requestData=new WebSocketRequestData();
        //初始化
        requestData.setIsOkDataProcessing(false);
        try {
            if(json!=null) {
                //将jsonText分解
                Map<String, String> mapJson = JSON.parseObject(json, new TypeReference<Map<String, String>>() {
                }.getType());
                if(mapJson!=null&& mapJson.get("text")!=null){
                    String jsonText=mapJson.get("text");
                    requestData.setJsonText(jsonText);
                    Map<String, String> mapText = JSON.parseObject(jsonText, new TypeReference<Map<String, String>>() {
                    }.getType());
                    if(mapText!=null && mapText.get("msg")!=null&& mapText.get("data")!=null ){
                        requestData.setJsonData(mapText.get("data"));
                        requestData.setJsonMsg(mapText.get("msg"));
                        requestData.setIsOkDataProcessing(true);
                    }
                }
            }
        }catch (Exception e){
            log.info("数据处理异常: "+e.getMessage());
        }
        return requestData;
    }

    public static WebSocketRequestDataMateResult getRequestDataMateResult(String jsonData, String rule){
        Map<String, String> map=new HashMap<>();
        WebSocketRequestDataMateResult requestDataMateResult=new WebSocketRequestDataMateResult();
        requestDataMateResult.setValid(false);
        try {
            if (jsonData != null){
                Map<String, String> data = JSON.parseObject(jsonData, new TypeReference<Map<String, String>>() {
                }.getType());
                if(data!=null && mate.get(rule)!=null){
                    for(String key:mate.get(rule)){
                        if(data.get(key)!=null ){
                            map.put(key,data.get(key));
                        }
                    }
                    if( mate.get(rule).length== map.size()){
                        requestDataMateResult.setValid(true);
                    }
                }
            }
        }catch (Exception e){
            log.info("getMapData 异常"+e.getMessage());
        }
        requestDataMateResult.setMapData(map);
        return requestDataMateResult;
    }
}
